<?php
App::uses('Component', 'Controller');

class MessageComponent extends Component
{

    public function sendSMS($msg, $send_no)
    {

        //return false;//Temporal

        if (trim($msg) == "") {
            return false;
        }
        //some may bave two numbers so slipt them and use the first one
        $fon_number = '';
        $cell = '';
        if (strpos($send_no, ',') !== false) {
            $num_arr = explode(',', $send_no);
            $fon_number = trim($num_arr[0]);
        } elseif (strpos($send_no, '/') !== false) {
            $num_arr = explode('/', $send_no);
            $fon_number = trim($num_arr[0]);
        } else {
            $fon_number = trim($send_no);
        }
        //remove the dash separator if any
        $fon_number = preg_replace('/-/', '', $fon_number);
        if (strlen($fon_number) < 9) { //not a mobile number
            return false;
        }
        $country_code = substr($fon_number, 0, 3);
        $cell = $fon_number;
        if ($country_code != '233') {
            $cell = '233' . substr($fon_number, 1);
        }

        //echo $cell;
        //return;
        //$msg ="Testing";
        //$send_no="0243569192";

        // return;

        $msg = urlencode($msg);
        $url = "http://121.241.242.114:8080/bulksms/bulksms?username=sob-upsa2013&password=123456&type=0&dlr=0&destination=" . $cell . "&source=UPSAALERT&message=" . $msg;
        //http://121.241.242.114:8080/bulksms/bulksms?username=sob-upsa2013&password=123456&type=0&dlr=0&destination=233243569192&source=upsa&message=testing
        // $result = @file_get_contents($url);
        $result = file_get_contents($url);
        $return = true;
        if ($result === false) {
            $return = false;
        }

        return $return;
    }
}